package com.glory.brepda.activity.mfg;

import androidx.recyclerview.widget.RecyclerView;

import android.os.Handler;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.glory.brepda.R;
import com.glory.brepda.adapter.EquipmentCombinationTwoBranchAdapter;
import com.glory.brepda.base.BaseActivity;
import com.glory.brepda.entity.MultimastereqpbindingBean;
import com.glory.brepda.entity.response.BaseResponseBean;
import com.glory.brepda.utils.NetUtils;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.model.Response;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_DialogEntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_EntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.view.FreeUI_ClearEditText;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class EquipmentCombinationTwoBranchActivity extends BaseActivity {

    @BindView(R.id.tvTitle)
    TextView tvTitle;
    @BindView(R.id.llMain)
    LinearLayout llMain;
    @BindView(R.id.tvBaseSubmit)
    TextView tvBaseSubmit;
    @BindView(R.id.lock)
    TextView lock;
    @BindView(R.id.unLock)
    TextView unLock;
    @BindView(R.id.eqpId)
    FreeUI_ClearEditText eqpId;
    @BindView(R.id.rveqpIDList)
    RecyclerView rveqpIDList;

    private EquipmentCombinationTwoBranchAdapter mEquipmentCombinationTwoBranchAdapter;
    private List<MultimastereqpbindingBean.EquipmentlistBean> eqpIdList = new ArrayList<>();

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_equipment_combination_two_branch;
    }

    @Override
    protected void initView() {

        setMainLayout(llMain);
        tvTitle.setText("设备组合(二分厂)");
        tvBaseSubmit.setVisibility(View.GONE);

        initRecyclerView(rveqpIDList, this);
        mEquipmentCombinationTwoBranchAdapter = new EquipmentCombinationTwoBranchAdapter(null);
        mEquipmentCombinationTwoBranchAdapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN);
        mEquipmentCombinationTwoBranchAdapter.isFirstOnly(true);
        rveqpIDList.setAdapter(mEquipmentCombinationTwoBranchAdapter);

    }

    @OnClick({R.id.lock,R.id.unLock})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.lock:
                if (mEquipmentCombinationTwoBranchAdapter.getData().size()<=0){
                    showToast("请添加设备编号",false);
                    return;
                }
                lock("MultiMasterEqpReset");
                break;
            case R.id.unLock:
                if (mEquipmentCombinationTwoBranchAdapter.getData().size()<=0){
                    showToast("请添加设备编号",false);
                    return;
                }
                lock("MultiMasterEqpEmpty");
                break;
        }
    }


    private void lock(String type){

        FreeUI_EntityCallBack<BaseResponseBean<MultimastereqpbindingBean>> callback = new FreeUI_DialogEntityCallBack<BaseResponseBean<MultimastereqpbindingBean>>
                (new TypeToken<BaseResponseBean<MultimastereqpbindingBean>>() {
                }.getType(), getSupportFragmentManager(), this) {

            @Override
            public void onSuccess(Response<BaseResponseBean<MultimastereqpbindingBean>> response) {
                super.onSuccess(response);
                if (response.body().isSuccess(getApplicationContext(), true, getSupportFragmentManager())) {

                    if (type.equals("MultiMasterEqpReset")){
                        showToast("绑定成功",true);
                    }else {
                        showToast("解绑成功",true);
                    }
                    new Handler().postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            finish();
                        }
                    }, 1000);

                }
            }

            @Override
            public void onError(Response<BaseResponseBean<MultimastereqpbindingBean>> response) {
                super.onError(response);
                loadError(response.getException(), "GETSTEPBYLOT");
            }
        };
        JsonObject map = new JsonObject();
        map.addProperty("ACTIONTYPE", type);
//        JsonArray jsonArray = new JsonArray();
//        for (MultimastereqpbindingBean.EquipmentlistBean equipmentId : mEquipmentCombinationTwoBranchAdapter.getData()) {
//            JsonObject jsonObject = new JsonObject();
//            jsonObject.addProperty("EQUIPMENTID", equipmentId.getEquipmentid());
//            jsonArray.add(jsonObject);
//        }
        map.add("EQUIPMENTLIST",new JsonParser().parse(new Gson().toJson(mEquipmentCombinationTwoBranchAdapter.getData())).getAsJsonArray());
        NetUtils.requestPostNet("IF.MULTIMASTEREQPBINDING", "Common", EquipmentCombinationTwoBranchActivity.this, map, callback);

    }


    @Override
    protected void initData() {

        initEdit(eqpId,s->{

            FreeUI_EntityCallBack<BaseResponseBean<MultimastereqpbindingBean>> callback = new FreeUI_DialogEntityCallBack<BaseResponseBean<MultimastereqpbindingBean>>
                    (new TypeToken<BaseResponseBean<MultimastereqpbindingBean>>() {
                    }.getType(), getSupportFragmentManager(), this) {

                @Override
                public void onSuccess(Response<BaseResponseBean<MultimastereqpbindingBean>> response) {
                    super.onSuccess(response);
                    if (response.body().isSuccess(getApplicationContext(), true, getSupportFragmentManager())) {
                        response.body().getResponse().getBody().getEquipmentlist().contains(mEquipmentCombinationTwoBranchAdapter.getData());
                        mEquipmentCombinationTwoBranchAdapter.addData(response.body().getResponse().getBody().getEquipmentlist());
                        eqpId.setText("");
                        new Handler().postDelayed(new Runnable() {
                            @Override
                            public void run() {
                                eqpId.requestFocus();
                            }
                        }, 500);
                    }
                }

                @Override
                public void onError(Response<BaseResponseBean<MultimastereqpbindingBean>> response) {
                    super.onError(response);
                    loadError(response.getException(), "GETSTEPBYLOT");
                }
            };
            JsonObject map = new JsonObject();
            map.addProperty("ACTIONTYPE", "MultiMasterEqpQuery");
            map.addProperty("EQUIPMENTID", s);
            NetUtils.requestPostNet("IF.MULTIMASTEREQPBINDING", "Common", EquipmentCombinationTwoBranchActivity.this, map, callback);


        });

    }

}